﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using DiscMaster.Web.Models;
using DiscMaster.Web.Extensions;

namespace DiscMaster.Web.Helpers
{
    public static class ProfileImageHelper
    {
        public static string ProfileImage(this HtmlHelper helper, Models.Player player, string alternateText, string width, string height)
        {
            return ProfileImage(helper, player, alternateText, width, height, null);
        }

        public static string ProfileImage(this HtmlHelper helper, Models.Player player, string alternateText, string width, string height, object htmlAttributes)
        {
            // Create tag builder
            var builder = new TagBuilder("img");

            // Create valid id
            builder.GenerateId(player.UserName);

            // Add attributes
            string format = "{0}/{1}";
            if (!string.IsNullOrEmpty(width))
            {
                format = format + "&width={2}";
            }
            if (!string.IsNullOrEmpty(height))
            {
                format = format + "&height={3}";
            }

            builder.MergeAttribute("src", string.Format(format,"/Image.ashx?src=/Content/Media/Images/Players/",player.PlayerDetail.profilepicture.IfEmptyThen("noprofilepicture.png"),width,height));
            builder.MergeAttribute("alt", alternateText.IfEmptyThen(player.UserName));
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

            // Render tag
            return builder.ToString(TagRenderMode.SelfClosing);
        }
    }
}
